﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LaoRomanise
{
    //http://search.cpan.org/~jokke/Lingua-LO-Romanize/

    // perl Lingua.docx
    class Syllable
    {
        Dictionary<string, string> BGN_PCGN = new Dictionary<string, string>();
        public Syllable()
        {
            BGN_PCGN.Add("ກ", "k");
            BGN_PCGN.Add("ຂ", "kh");
            BGN_PCGN.Add("ຄ", "kh");
            BGN_PCGN.Add("ງ", "ng");
            BGN_PCGN.Add("ຈ", "ch");
            BGN_PCGN.Add("ສ", "s");
            BGN_PCGN.Add("ຊ", "x");
            BGN_PCGN.Add("ຕ", "t");
            BGN_PCGN.Add("ຖ", "th");
            BGN_PCGN.Add("ທ", "th");
            BGN_PCGN.Add("ນ", "n");
            BGN_PCGN.Add("ໜ", "n");
            BGN_PCGN.Add("ປ", "p");
            BGN_PCGN.Add("ຜ", "ph");
            BGN_PCGN.Add("ຝ", "f");
            BGN_PCGN.Add("ພ", "ph");
            BGN_PCGN.Add("ຟ", "f");
            BGN_PCGN.Add("ມ", "m");
            BGN_PCGN.Add("ໝ", "m");
            BGN_PCGN.Add("ຢ", "y");
            BGN_PCGN.Add("ຣ", "r");
            BGN_PCGN.Add("ຣ໌", "r");
            BGN_PCGN.Add("ລ", "l");
            BGN_PCGN.Add("ຼ", "l");
            BGN_PCGN.Add("ຫ", "h");
            BGN_PCGN.Add("ຮ", "h");

            //#ຍ, ຽ, ດ, ບ, ວ, ອ special cases
            //#vowels

            BGN_PCGN.Add("ະ", "a");
            BGN_PCGN.Add("ັ", "a");
            BGN_PCGN.Add("າ", "a");
            BGN_PCGN.Add("ິ", "i");
            BGN_PCGN.Add("ີ", "i");
            BGN_PCGN.Add("ຶ", "u");
            BGN_PCGN.Add("ື", "u");
            BGN_PCGN.Add("ຸ", "ou");
            BGN_PCGN.Add("ູ", "ou");
            BGN_PCGN.Add("ເະ", "é");
            BGN_PCGN.Add("ເັ", "é");
            BGN_PCGN.Add("ເ", "é");
            BGN_PCGN.Add("ແະ", "è");
            BGN_PCGN.Add("ແັ", "è");
            BGN_PCGN.Add("ແ", "è");
            BGN_PCGN.Add("ໂະ", "ô");
            BGN_PCGN.Add("ົ", "ô");
            BGN_PCGN.Add("ໂ", "ô");
            BGN_PCGN.Add("ເາະ  ", "o");
            BGN_PCGN.Add("ັອ", "o");
            BGN_PCGN.Add("ໍ", "o");
            BGN_PCGN.Add("ອ", "o");
            BGN_PCGN.Add("ເັຽະ ", "ia");
            BGN_PCGN.Add("ັຽ", "ia");
            BGN_PCGN.Add("ເັຽ  ", "ia");
            BGN_PCGN.Add("ຽ", "ia");
            BGN_PCGN.Add("ເັຍະ ", "ia"); //#?
            BGN_PCGN.Add("ັຍ", "ia"); //#?
            BGN_PCGN.Add("ເັຍ  ", "ia"); //#?
            BGN_PCGN.Add("ຍ", "ia"); //#?
            BGN_PCGN.Add("ເຍ", "ia");
            BGN_PCGN.Add("ເຶອະ ", "ua");
            BGN_PCGN.Add("ເຶອ  ", "ua");
            BGN_PCGN.Add("ເືອ  ", "ua");
            BGN_PCGN.Add("ເິະ  ", "eu");
            BGN_PCGN.Add("ເິ", "eu");
            BGN_PCGN.Add("ເີ", "eu");
            BGN_PCGN.Add("ເື", "eu");
            BGN_PCGN.Add("ໄ", "ai");
            BGN_PCGN.Add("ໃ", "ai");
            BGN_PCGN.Add("ເົາ  ", "ao");
            BGN_PCGN.Add("ຳ", "am"); //not needed?
            BGN_PCGN.Add("ໍາ", "am"); //not needed?
            BGN_PCGN.Add("ິວ", "iou");
            BGN_PCGN.Add("ີວ", "iou");
            //# special case: ົວະ, ັວ, ົວ, ວ


        }

        bool isSyllablestr(string iss, string isa, int required)
        {
            return iss == "ro" && isa == "Str" && required == 1;
        }

        string RemoveRangeOfChars(string input, char first, char last)
        {
            int charcode = first;
            while (charcode <= last)
            {

                input.Replace("" + (char)charcode, "");
                charcode++;
            }
            return input;
        }

        bool IsRange(char test, char first, char last)
        {
            return test >= first && test <= last;
        }

        void romanize(string syllable_str)
        {
            syllable_str = RemoveRangeOfChars(syllable_str, '່', '໋');
            syllable_str.Replace("ຯ", "...");
            syllable_str = SubstituteNumbers(syllable_str);

            string romanized_str = "";
            foreach (char ch in syllable_str)
            {
                switch(ch)
                {
                    case 'ຍ':
                    case 'ຽ':
                        romanized_str += "gn";
                        break;

                    case 'ດ':
                        romanized_str += "d";
                        break;

                    case 'ບ':
                        romanized_str += "b";
                        break;

                    case 'ວ':
                        romanized_str += "v";
                        break;

                    case 'ອ':
                        romanized_str += "-";
                        break;

                    default:
                        if (BGN_PCGN.ContainsKey(""+ch))
                        {
                            romanized_str += BGN_PCGN[""+ch];
                        }
                        break;
                }

            }

        }

        private string SubstituteNumbers(string syllableStr)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char ch in syllableStr)
            {
                if (IsRange(ch, '໐', '໙'))
                {
                    sb.Append((char)(ch - 3792));
                }
                else
                {
                    sb.Append(sb);
                }
            }
            return sb.ToString();

        }
    }
}
